package controllers;

import java.util.ArrayList;
import java.util.List;


import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.ChubCountryMapping;
import myDaos.MappingDao;

@ManagedBean
@ViewScoped
public class MappingManagedBean {

	private List<ChubCountryMapping> mlist;
	private String country;
	private String distributor;
	private boolean isMethodCalled = false;
	private List<SelectItem> distList;


	@EJB
	private MappingDao md;
	 
	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;
	
	
	
	

	public List<SelectItem> getDistList() {
		return distList;
	}


	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}

	

	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}


	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}


	public boolean isMethodCalled() {
		return isMethodCalled;
	}


	public void setMethodCalled(boolean isMethodCalled) {
		this.isMethodCalled = isMethodCalled;
	}


	public List<ChubCountryMapping> getMlist() {
		return mlist;
	}


	public void setMlist(List<ChubCountryMapping> mlist) {
		this.mlist = mlist;
	}
	


	public String getDistributor() {
		return distributor;
	}


	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}



	public void updateList()
	{
		mlist = md.getMappingsByFilter(country, distributor);
		isMethodCalled=true;
	}
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
	    {  
			if(country!=null && !country.trim().equals("") && 
					  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
			{
			distList.add(new SelectItem(
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
										 ,
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
					                    )
			             );
			}
	    }
	}

}
